package com.example.springsecuritydemo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @author 朱学鑫
 * @projectName springSecurity-Demo
 * @description: SwaggerConfig
 * @date 2022年03月17日 16:36
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/swagger-ui/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/");
    }


    /**
     * 通过api函数来构建一个DocketBean
     * 函数名可随意命名
     * @return
     */
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .enable(true)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.springsecuritydemo.controller"))
                .paths(PathSelectors.any())
                .build()
                .apiInfo(aipInfo());
    }

    //用来创建该API的基本信息，这些信息会展示在文档页面当中
    private ApiInfo aipInfo() {
        return  new ApiInfoBuilder()
                .title("swagger平台")
                .description("swagger平台 API helper")
                .version("v-0.2.0")
                .build();
    }
}
